import 'package:flutter/material.dart';
import 'package:message_tools/application.dart';
import 'package:message_tools/components/index.dart';
import 'package:message_tools/router/index.dart';
import 'package:message_tools/store/index.dart';
import 'package:message_tools/store/view_model/index.dart';
import 'package:message_tools/store/view_model/sms_template_model.dart';

class SelectSmsTemplatePage extends StatefulWidget {
  _SelectSmsTemplatePageState createState() => _SelectSmsTemplatePageState();
}

class _SelectSmsTemplatePageState extends State<SelectSmsTemplatePage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;
  bool isLoadData = false;

  @override
  Widget build(BuildContext context) {
    super.build(context);

    if (!isLoadData) {
      Store.value<SmsTemplateModel>(context).loadData();
      isLoadData = true;
    }

    return Scaffold(
        appBar: AppBarPlugin.Create(
          "短信模板",
          centerTitle: true,
          leading: Builder(builder: (BuildContext c) {
            return IconButton(
                icon: const Icon(Icons.close),
                onPressed: () {
                  Navigator.pop(context);
                });
          }),
          actions: <Widget>[
            IconButton(
                icon: Image.asset('assets/images/add.png'),
                onPressed: () {
                  var route = Routes.RouteAddSmsTemplatePage;
                  Application.router.navigateTo(context, route,
                      transition: TransitionType.fadeIn);
                })
          ],
        ),
        body: Container(
            color: Colors.grey[100],
            child: Store.connect<SmsTemplateModel>(
                builder: (context, snapshot, child) {
              return _buildListViewItem(snapshot.list);
            })));
  }

  Widget _buildListViewItem(List list) {
    return ListView.builder(
      scrollDirection: Axis.vertical,
      itemCount: list.length, // item 的个数
      itemBuilder: (BuildContext context, int index) {
        var smsTemplate = list[index];

        return GestureDetector(
          onTap: () {
            /*Store.value<SmsTaskModel>(context)
                .setSelectSmsTemplate(smsTemplate);*/
            Navigator.pop(context, Application.jsonEncode(smsTemplate));
          },
          child: Container(
            margin: const EdgeInsets.all(5),
            padding: const EdgeInsets.all(10),
            decoration: new BoxDecoration(
              //背景
              color: Colors.white,
              //设置四周边框
              border: createBorder(color: Colors.grey[300]),
            ),
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Text(smsTemplate.title,
                        style: TextStyle(
                            height: 1,
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                            color: Colors.grey[600])),
                  ],
                ),
                Divider(
                  height: 10.0,
                  indent: 0.0,
                  color: Colors.grey,
                ),
                Row(
                  children: <Widget>[
                    LimitedBox(
                        maxWidth: MediaQuery.of(context).size.width - 32,
                        child: Text(
                          smsTemplate.content,
                          softWrap: true,
                          overflow: TextOverflow.ellipsis,
                          maxLines: 100,
                          textAlign: TextAlign.left,
                          style: new TextStyle(
                              height: 1,
                              fontSize: 16,
                              color: Colors.grey[700],
                              letterSpacing: 1.2),
                        ))
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
